package com.oracle.outsidein;

import com.oracle.outsidein.pool.ConvertResult;
import com.oracle.outsidein.pool.ConvertTaskInfo;
import com.oracle.outsidein.pool.ProcessManager;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Slf4j
@Data
public class OutsideinManager {
    private OutsideinManagerConfig config;
    private ProcessManager processManager;

    public OutsideinManager(OutsideinManagerConfig config) {
        this.config = config;
    }

    public static OutsideinManagerConfig config() {
        return new OutsideinManagerConfig();
    }

    // 启动
    public void startup() {
        log.info("OutsideinManager startup...");
        processManager = new ProcessManager(config);
        processManager.startup();
    }

    // 停止
    public void shutdown() {
        log.info("OutsideinManager shutdown...");
        processManager.shutdown();
    }

    public boolean isRun() {
        return processManager.isRun();
    }

    // 执行任务
    public ConvertResult execute(ConvertTaskInfo info) throws ExecutionException, InterruptedException {
        if (!processManager.isRun()) {
            throw new RuntimeException("startup OutsideinManager before");
        }
        log.info("OutsideinManager execute...");
        Future<ConvertResult> future = processManager.execute(info);
        return future.get();
    }
}
